//
// Created by 张易生 on 2022/1/14.
//

#ifndef ZYSPLAYER_ZYSVIDEOPLAYER_H
#define ZYSPLAYER_ZYSVIDEOPLAYER_H

#include "AndroidLog.h"
#include "ZYSPlayStatus.h"
#include "ZYSQueue.h"
#include "ZYSCallJava.h"
#include "ZYSAudioPlayer.h"

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavutil/time.h"
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
};
class ZYSVideoPlayer {

public:
    int streamIndex = -1;
    AVCodecContext *avCodecContext = nullptr;
    AVCodecParameters *codecpar = nullptr;
    ZYSQueue *queue = nullptr;
    ZYSPlayStatus *playStatus = nullptr;
    //解码时用的锁
    pthread_mutex_t codecMutex;
    ZYSCallJava *callJava = NULL;

    int data_size = 0;
    pthread_t thread_play;
    AVPacket *avPacket = NULL;
    AVFrame *avFrame = NULL;
    int ret = 0;

    //总时长
    int duration = 0;
    //当前播放时间
    double clock = 0;
    //实时计算出来的休眠时间
    double delayTime = 0;
    //    默认休眠时间 40ms  0.04s ：标准帧率25帧
    double defaultDelayTime = 0.04;
    ZYSAudioPlayer *audio = NULL;
    AVRational time_base;


public:
    ZYSVideoPlayer(ZYSPlayStatus *playStatus, ZYSCallJava *callJava);
    ~ZYSVideoPlayer();
    //开始播放
    void play();

    //暂停
    void pause();

    //恢复
    void resume();

    void release();

    void playVideo_cpp();

    double getFrameDiffTime(AVFrame *avFrame);

    double getDelayTime(double diff);
};


#endif //ZYSPLAYER_ZYSVIDEOPLAYER_H
